//
//  HMEmoticonModel.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMEmoticonModel: NSObject, NSCoding {
    
    var id: String
    
//    emoji表情使用
    var code: String?
    {
        didSet
        {
            let scanner = NSScanner(string: code!)
            var result: UInt32 = 0
            
            scanner.scanHexInt(&result)
            
            let scalar = UnicodeScalar(result)
            let c = Character(scalar)
            
            emoji = String(c)
        }
    }
    
    var emoji: String?
    
    ///中文名称
    var chs: String?
    
    ///图片名称
    var png: String?
    {
        didSet
        {
//            图片的全路径 = mainBundle/id/png
            fullPngPath = emoticonsBundlePath + "/" + id + "/" + png!
        }
    }
    
    var fullPngPath: String?
    
    init(dict: [String: String], id: String) {
        self.id = id
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description:String
    {
        get
        {
            return "\n\t\t:表情模型: chs:\(chs), png: \(png), code:\(code)"
        }
    }
    
//    归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        self.id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        self.chs = aDecoder.decodeObjectForKey("chs") as? String
        self.code = aDecoder.decodeObjectForKey("code") as? String
        self.png = aDecoder.decodeObjectForKey("png") as? String
        self.emoji = aDecoder.decodeObjectForKey("emoji") as? String
        super.init()
        
        if self.png != nil
        {
            self.fullPngPath = emoticonsBundlePath + "/" + id + "/" + png!
        }
    }
    
    ///将一个表情模型中的图片专程带附件的属性文本
    func emoticonModelToAttrText(font: UIFont) ->NSAttributedString
    {
        guard let fullPngPath = self.fullPngPath else
        {
            return NSAttributedString(string: "")
        }
        
        let attachment = HMTextAttachment()
        attachment.image = UIImage(named: fullPngPath)
        
//        把模型对应的名称发到附件中
        attachment.chs = chs
        
//        设置附件的bounds
        let wh = font.lineHeight ?? 15
//        bounds.y和frame.y,参照系不一样.bounds往下走是负数
        attachment.bounds = CGRect(x: 0, y: -5, width: wh, height: wh)
        
        
        let attrM = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
//        附件中没有font属性跟在他后面的图片就会变的很小
        attrM.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        return attrM
        
    }
    
    ///根据表情名称找模型
    class func emoticonNameToEmoticonModel(name: String)->HMEmoticonModel? {
        
        for packageModel in HMEmoticonManager.sharedManager.packageModels
        {
            for emoticon in packageModel.emoticons
            {
                if emoticon.chs == name
                {
                    return emoticon
                }
            }
        }
        return nil
    }
}









